﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Resources;
using System.Reflection;

namespace UserInterface
{
    using BusinessLogicLayer;
    using BusinessObjectLayer;
    using System.Net;
    using System.IO;

    public partial class ShowCategoryItemUI : Form
    {
        CategoryItemBLL _CategoryItemBLL;
        ExceptionHandlerBOL _ExceptionHandlerBOL;
        private AccessMode _AccessMode;
        public ShowCategoryItemUI(AccessMode accessMode)
        {
            InitializeComponent();
            newCategoryItemButton.Enabled = (accessMode != AccessMode.Read);
            _AccessMode = accessMode;
            _CategoryItemBLL = new CategoryItemBLL();
            fillDataGridView();
        }
        private void fillDataGridView()
        {
            try
            {
                CategoryItemBOL[] CategoryItemRecords = _CategoryItemBLL.Select();
                CategoryItemDataGridView.Rows.Clear();
                if (CategoryItemRecords != null)
                    for (int index = 0; index < CategoryItemRecords.Length; index++)
                    {
                        CategoryItemDataGridView.Rows.Add();
                        DataGridViewRow row = CategoryItemDataGridView.Rows[index];
                        row.Cells["categoryItemId"].Value = CategoryItemRecords[index].CategoryItemId;
                        row.Cells["categoryItemTitle"].Value = CategoryItemRecords[index].CategoryItemTitle;
                        row.Cells["description"].Value = CategoryItemRecords[index].Description;
                        row.Cells["categoryId"].Value = CategoryItemRecords[index].CategoryId;
                        row.Cells["categoryTitle"].Value = CategoryItemRecords[index].CategoryTitle;                        
                    }
            }
            catch (Exception exception)
            {
                _ExceptionHandlerBOL = ExceptionHandlerBLL.HandleException(exception);
                messageTitleLabel.ForeColor = Color.Red;
                messageTitleLabel.Text = _ExceptionHandlerBOL.Title;
            }
        }
        private void CategoryItemDataGridView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            messageTitleLabel.Text = string.Empty;
            if (e.RowIndex != -1 && _AccessMode == AccessMode.Complete)
                ItemSelected(e.RowIndex);
            if (_AccessMode != AccessMode.Complete)
            {
                _ExceptionHandlerBOL = ExceptionHandlerBLL.HandleException(new Exception("NoAccess"));
                messageTitleLabel.ForeColor = Color.Red;
                messageTitleLabel.Text = _ExceptionHandlerBOL.Title;
            }
        }
        private void ItemSelected(int selectedIndex)
        {
            short categoryItemId = Convert.ToInt16(CategoryItemDataGridView.Rows[selectedIndex].Cells["categoryItemId"].Value);
            string categoryItemTitle = (CategoryItemDataGridView.Rows[selectedIndex].Cells["categoryItemTitle"].Value == null ? null : CategoryItemDataGridView.Rows[selectedIndex].Cells["categoryItemTitle"].Value.ToString());
            string description = (CategoryItemDataGridView.Rows[selectedIndex].Cells["description"].Value == null ? null : CategoryItemDataGridView.Rows[selectedIndex].Cells["description"].Value.ToString());
            short categoryId = Convert.ToInt16(CategoryItemDataGridView.Rows[selectedIndex].Cells["categoryId"].Value);
            string categoryTitle = (CategoryItemDataGridView.Rows[selectedIndex].Cells["categoryTitle"].Value == null ? null : CategoryItemDataGridView.Rows[selectedIndex].Cells["categoryTitle"].Value.ToString());
            CategoryItemBOL _CategoryItemBOL = new CategoryItemBOL(categoryItemId, categoryItemTitle, description,categoryId,categoryTitle);
            new EditCategoryItemUI(_CategoryItemBOL).ShowDialog();
            fillDataGridView();
        }
        private void CategoryItemdataGridView_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == '\r')
                if (CategoryItemDataGridView.Rows.Count != 0)
                    ItemSelected(CategoryItemDataGridView.CurrentRow.Index);
        }
        private void closeForm()
        {
            this.Close();
        }
        private void newCategoryItemButton_Click(object sender, EventArgs e)
        {
            new EditCategoryItemUI().ShowDialog();
            fillDataGridView();
        }
        private void ShowCategoryItem_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
                closeForm();
        }
        private void messageTitleLabel_Click(object sender, EventArgs e)
        {
            if (_ExceptionHandlerBOL != null)
                new ExceptionHandlerUI(_ExceptionHandlerBOL).ShowDialog();
            _ExceptionHandlerBOL = null;
        }
    }
}